package com.openbravo.data.user;

import com.openbravo.basic.BasicException;
import com.openbravo.data.loader.LocalRes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.EventListener;
import java.util.List;
import javax.swing.ListModel;
import javax.swing.event.EventListenerList;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

public class BrowsableData
  implements ListModel
{
  protected EventListenerList listeners = new EventListenerList();
  private boolean m_bIsAdjusting;
  private ListProvider m_dataprov;
  private SaveProvider m_saveprov;
  public SaveProvider getM_saveprov() {
	return m_saveprov;
}

public void setM_saveprov(SaveProvider m_saveprov) {
	this.m_saveprov = m_saveprov;
}

private List m_aData;
  private Comparator m_comparer;

  public BrowsableData(ListProvider paramListProvider, SaveProvider paramSaveProvider, Comparator paramComparator)
  {
    this.m_dataprov = paramListProvider;
    this.m_saveprov = paramSaveProvider;
    this.m_comparer = paramComparator;
    this.m_bIsAdjusting = false;
    this.m_aData = new ArrayList();
  }

  public BrowsableData(ListProvider paramListProvider, SaveProvider paramSaveProvider)
  {
    this(paramListProvider, paramSaveProvider, null);
  }

  public BrowsableData(ListProvider paramListProvider)
  {
    this(paramListProvider, null, null);
  }

  public final void addListDataListener(ListDataListener paramListDataListener)
  {
    this.listeners.add(ListDataListener.class, paramListDataListener);
  }

  public final void removeListDataListener(ListDataListener paramListDataListener)
  {
    this.listeners.remove(ListDataListener.class, paramListDataListener);
  }

  public final Object getElementAt(int paramInt)
  {
    return this.m_aData.get(paramInt);
  }

  public final int getSize()
  {
    return this.m_aData.size();
  }

  public final boolean isAdjusting()
  {
    return this.m_bIsAdjusting;
  }

  protected void fireDataIntervalAdded(int paramInt1, int paramInt2)
  {
    this.m_bIsAdjusting = true;
    EventListener[] arrayOfEventListener = this.listeners.getListeners(ListDataListener.class);
    ListDataEvent localListDataEvent = null;
    for (int i = 0; i < arrayOfEventListener.length; i++)
    {
      if (localListDataEvent == null)
        localListDataEvent = new ListDataEvent(this, 1, paramInt1, paramInt2);
      ((ListDataListener)arrayOfEventListener[i]).intervalAdded(localListDataEvent);
    }
    this.m_bIsAdjusting = false;
  }

  protected void fireDataContentsChanged(int paramInt1, int paramInt2)
  {
    this.m_bIsAdjusting = true;
    EventListener[] arrayOfEventListener = this.listeners.getListeners(ListDataListener.class);
    ListDataEvent localListDataEvent = null;
    for (int i = 0; i < arrayOfEventListener.length; i++)
    {
      if (localListDataEvent == null)
        localListDataEvent = new ListDataEvent(this, 0, paramInt1, paramInt2);
      ((ListDataListener)arrayOfEventListener[i]).contentsChanged(localListDataEvent);
    }
    this.m_bIsAdjusting = false;
  }

  protected void fireDataIntervalRemoved(int paramInt1, int paramInt2)
  {
    this.m_bIsAdjusting = true;
    EventListener[] arrayOfEventListener = this.listeners.getListeners(ListDataListener.class);
    ListDataEvent localListDataEvent = null;
    for (int i = 0; i < arrayOfEventListener.length; i++)
    {
      if (localListDataEvent == null)
        localListDataEvent = new ListDataEvent(this, 2, paramInt1, paramInt2);
      ((ListDataListener)arrayOfEventListener[i]).intervalRemoved(localListDataEvent);
    }
    this.m_bIsAdjusting = false;
  }

  public void refreshData()
    throws BasicException
  {
    putNewData(this.m_dataprov == null ? null : this.m_dataprov.refreshData());
  }

  public void loadData()
    throws BasicException
  {
    putNewData(this.m_dataprov == null ? null : this.m_dataprov.loadData());
  }

  public void unloadData()
    throws BasicException
  {
    putNewData(null);
  }

  public void loadList(List paramList)
  {
    putNewData(paramList);
  }

  public void sort(Comparator paramComparator)
    throws BasicException
  {
    Collections.sort(this.m_aData, paramComparator);
    putNewData(this.m_aData);
  }

  public final boolean canLoadData()
  {
    return this.m_dataprov != null;
  }

  public boolean canInsertData()
  {
    return (this.m_saveprov != null) && (this.m_saveprov.canInsert());
  }

  public boolean canDeleteData()
  {
    return (this.m_saveprov != null) && (this.m_saveprov.canDelete());
  }

  public boolean canUpdateData()
  {
    return (this.m_saveprov != null) && (this.m_saveprov.canUpdate());
  }

  public final int findNext(int paramInt, Finder paramFinder)
    throws BasicException
  {
    for (int i = paramInt + 1; i < this.m_aData.size(); i++)
      if (paramFinder.match(getElementAt(i)))
        return i;
    for (int i = 0; i < paramInt; i++)
      if (paramFinder.match(getElementAt(i)))
        return i;
    return -1;
  }

  public final int removeRecord(int paramInt)
    throws BasicException
  {
    if ((canDeleteData()) && (paramInt >= 0) && (paramInt < this.m_aData.size()))
    {
      if (this.m_saveprov.deleteData(getElementAt(paramInt)) > 0)
      {
        this.m_aData.remove(paramInt);
        fireDataIntervalRemoved(paramInt, paramInt);
        int i;
        if (paramInt < this.m_aData.size())
          i = paramInt;
        else
          i = this.m_aData.size() - 1;
        return i;
      }
      throw new BasicException(LocalRes.getIntString("exception.nodelete"));
    }
    throw new BasicException(LocalRes.getIntString("exception.nodelete"));
  }

  public final int updateRecord(int paramInt, Object paramObject)
    throws BasicException
  {
    if ((canUpdateData()) && (paramInt >= 0) && (paramInt < this.m_aData.size()))
    {
      if (this.m_saveprov.updateData(paramObject) > 0)
      {
        int i;
        if (this.m_comparer == null)
        {
          i = paramInt;
          this.m_aData.set(i, paramObject);
        }
        else
        {
          i = insertionPoint(paramObject);
          if (i == paramInt + 1)
          {
            i = paramInt;
            this.m_aData.set(i, paramObject);
          }
          else if (i > paramInt + 1)
          {
            this.m_aData.remove(paramInt);
            i--;
            this.m_aData.add(i, paramObject);
          }
          else
          {
            this.m_aData.remove(paramInt);
            this.m_aData.add(i, paramObject);
          }
        }
        if (i >= paramInt)
          fireDataContentsChanged(paramInt, i);
        else
          fireDataContentsChanged(i, paramInt);
        return i;
      }
      throw new BasicException(LocalRes.getIntString("exception.noupdate"));
    }
    throw new BasicException(LocalRes.getIntString("exception.noupdate"));
  }

  public final int insertRecord(Object paramObject)
    throws BasicException
  {
    if ((canInsertData()) && (this.m_saveprov.insertData(paramObject) > 0))
    {
      int i;
      if (this.m_comparer == null)
        i = this.m_aData.size();
      else
        i = insertionPoint(paramObject);
      this.m_aData.add(i, paramObject);
      fireDataIntervalAdded(i, i);
      return i;
    }
    throw new BasicException(LocalRes.getIntString("exception.noinsert"));
  }

  private final void putNewData(List paramList)
  {
    int i = this.m_aData.size();
    this.m_aData = (paramList == null ? new ArrayList() : paramList);
    int j = this.m_aData.size();
    if (this.m_comparer != null)
      Collections.sort(this.m_aData, this.m_comparer);
    fireDataContentsChanged(0, j - 1);
    if (i > j)
      fireDataIntervalRemoved(j, i - 1);
    else if (i < j)
      fireDataIntervalAdded(i, j - 1);
  }

  private final int insertionPoint(Object paramObject)
  {
    int i = 0;
    int j = this.m_aData.size() - 1;
    while (i <= j)
    {
      int k = i + j >> 1;
      Object localObject = this.m_aData.get(k);
      int m = this.m_comparer.compare(localObject, paramObject);
      if (m <= 0)
        i = k + 1;
      else
        j = k - 1;
    }
    return i;
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.data.user.BrowsableData
 * JD-Core Version:    0.6.0
 */